# Maintainer: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
# Contributor: Marian Buschsieweke <marian.buschsieweke@ovgu.de>
pkgname=ghidra
pkgver=10.4
pkgrel=1
pkgdesc="A Cross Platform and Open Source Electronics Design Automation Suite"
url="https://ghidra-sre.org/"
# only x86_64 and aarch64 support provided by upstream
# aarch64: jdk segfaults
arch="x86_64"
license="Apache-2.0"
makedepends="
	bash
	gradle
	protoc
	unzip
	"
# Note: Ghidra refuses to launch with only JRE installed, so
#       please do not move JDK to makedepends
depends="
	openjdk17-jdk
	wmname
	"
subpackages="
	$pkgname-tutorials
	$pkgname-doc
	"
source="
	https://github.com/NationalSecurityAgency/ghidra/archive/refs/tags/Ghidra_${pkgver}_build.tar.gz
	use-system-protoc.patch
	ghidra.sh
	ghidra.desktop
	"
# This contains binary executables in the exercises section of the
# documentation. We don't want to spoil the fun by stripping them.
options="net !check"
builddir="$srcdir/ghidra-Ghidra_${pkgver}_build"

prepare() {
	default_prepare

	local protoc_ver
	protoc_ver="$(protoc --version | grep -o '[0-9][.0-9]*')"

	# protoc version needs to match protobuf runtime version
	sed \
		-e "s/com.google.protobuf:protobuf-java:3.17.3/com.google.protobuf:protobuf-java:$protoc_ver/g" \
		-i Ghidra/Debug/Debugger-gadp/build.gradle
	sed \
		-e "s!lib/protobuf-java-3.17.3.jar!lib/protobuf-java-$protoc_ver.jar!g" \
		-i Ghidra/Debug/Debugger-gadp/Module.manifest
}

build() {
	export GRADLE_USER_HOME="$srcdir"/.gradle
	gradle --parallel -I gradle/support/fetchDependencies.gradle init
	gradle --parallel buildGhidra
}

package() {
	# impedance matching between different arch naming conventions:
	local arch
	case "$CARCH" in
		aarch64)
			arch="arm_64"
			;;
		*)
			arch="$CARCH"
			;;
	esac

	install -d "$pkgdir"/usr/share "$pkgdir"/usr/bin
	unzip \
		-u "build/dist/ghidra_${pkgver}_DEV_$(date +"%Y%m%d")_linux_$arch.zip" \
		-d "$pkgdir"/usr/share
	mv "$pkgdir"/usr/share/ghidra_${pkgver}_DEV "$pkgdir"/usr/share/ghidra

	# remove precompiled 7zip native libs for x86_64 Mac, Windows, and glibc
	# Linux. Ghidra is falling back to using a java implementation on musl
	# anyway.
	rm -rf "$pkgdir"/usr/share/ghidra/Ghidra/Features/FileFormats/data/sevenzipnativelibs

	install -Dm755 "$srcdir"/ghidra.sh "$pkgdir"/usr/bin/ghidra
	ln -s /usr/share/ghidra/support/analyzeHeadless "$pkgdir"/usr/bin/ghidra-headless
	install -Dm644 \
		Ghidra/Framework/Gui/src/main/resources/images/GhidraIcon64.png \
		"$pkgdir"/usr/share/icons/hicolor/64x64/apps/$pkgname.png
	install -Dm 644 $srcdir/ghidra.desktop -t $pkgdir/usr/share/applications
}

tutorials() {
	pkgdesc="Ghidra tutorial files"
	# dependency dump:
	#  openjdk17-jdk
	#  wmname
	#  so:libgcc_s.so.1
	#  so:libstdc++.so.6
	# the tutorials subpackage contains exmaples linked against glibc
	# which are ment to be decompiled by ghidra
	options="!strip !tracedeps"

	amove usr/share/ghidra/docs/GhidraClass
}

doc() {
	default_doc

	amove usr/share/ghidra/docs
}

sha512sums="
e7327b652ddb9dcb9025ef6db46d60e853bcc155cb493cf5dfd941a2c468e55a1be7d8aa5c4b2199a39e09653e69eba0a80347596f32de0440edf75cbea17cbc  Ghidra_10.4_build.tar.gz
504f3b7f0c571086e65275140e5eabca7e1d7cc58446b3e11f55e1539afddc8fe12cf3a98d3c60fcae1f27a84e2a8e42ce15fce78e67c2de93601d56a98c4f61  use-system-protoc.patch
b075d5ac821bf4ed052acada45b882876a5493ab03cfa4d0537c4c5f27b666aa2b37de9ff561313b025f9d16b06b9ad17d59287ecf205c02798dbca2bc04925f  ghidra.sh
a85b8b3276e2ff4ed8bda6470c15d02711ebaa48463c775cd2a36549fad738e9fe073dab80f8c57646490ffc959cdc27e9d25b1dc2a5810b0ddb249b5dc99a9b  ghidra.desktop
"
